package com.wzdigit.wms.wms.service.task;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.client.response.task.AsnQtyCheck;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.enums.AsnDtlStatusEnum;
import com.wzdigit.wms.basic.enums.AsnHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.AsnTypeEnum;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.common.utils.QueryInUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务-更新收获单状态
 * Created by zyf on 2021/8/30 10:28
 */
@Service
@Slf4j
public class AsnStatusTaskService {

    @Autowired
    private AsnHeaderMapper asnHeaderMapper;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void asnStatusTask(){
        log.info("-----定时任务更新收获单状态开始-----");
        List<AsnQtyCheck> list = asnHeaderMapper.selectQty(AsnHeaderStatusEnum.PARTRECEIVE.code, AsnTypeEnum.R01.code);
        if (CollectionUtil.isEmpty(list)){
            log.info("-----定时任务更新收获单状态结束，更新0条-----");
            return;
        }
        /*list=list.stream().filter(t -> t.getOrderQty().equals(t.getRcvQty())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(list)){
            log.info("-----定时任务更新收获单状态结束，更新0条-----");
        }*/
        int size = list.size();
        List<BigDecimal> idList = list.stream().map(t -> t.getAsnHeaderId()).collect(Collectors.toList());
        List<List<BigDecimal>> updateIdsList = QueryInUtil.splitInParams(999, idList);
        for (List<BigDecimal> updateIds : updateIdsList) {
            asnHeaderMapper.updateStatusByIdNew(updateIds,AsnHeaderStatusEnum.WHOLERECEIVE.code,"system");
        }
        log.info("-----定时任务更新收获单状态结束，更新"+size+"条-----");
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void asnStatusTaskNew(){
/*        List<Integer> statusList=new ArrayList<>();
        statusList.add(AsnDtlStatusEnum.CREATE.code);statusList.add(AsnDtlStatusEnum.PARTRECEIVE.code);
        log.info("-----定时任务更新收获单状态开始-----");
        List<BigDecimal> idList = asnHeaderMapper.selectCheckHeaderId(statusList, AsnTypeEnum.R01.code);
        if (CollectionUtil.isEmpty(idList)){
            log.info("-----定时任务更新收获单状态结束，更新0条-----");
            return;
        }
        *//*list=list.stream().filter(t -> t.getOrderQty().equals(t.getRcvQty())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(list)){
            log.info("-----定时任务更新收获单状态结束，更新0条-----");
        }*//*

        List<List<BigDecimal>> updateIdsList = QueryInUtil.splitInParams(999, idList);
        for (List<BigDecimal> updateIds : updateIdsList) {
            asnHeaderMapper.updateStatusByIdNew(updateIds,AsnHeaderStatusEnum.WHOLERECEIVE.code,"system");
            asnHeaderMapper.updateDtlStatusByIdNew(updateIds,AsnDtlStatusEnum.WHOLERECEIVE.code,"system");
        }
        log.info("-----定时任务更新收获单状态结束，更新"+idList.size()+"条-----");*/
    }



}
